package com.ym.medical.domain.bo.msg;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.util.List;

/**
 * @Author: DaiKuangYin
 * @Date: 2020/1/7 10:51
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class MpTemplate {
    private String touser;
    private String template_id;
    private String url;
    private Miniprogram miniprogram; //跳小程序所需数据，不需跳小程序可不用传该数据
    private String appid;
    private String pagepath ;   //所需跳转到小程序的具体页面路径，支持带参数,（示例index?foo=bar）
    private List<MpTemplateParam> data;
    private String color;

    public MpTemplate(String touser, String template_id, String url, String appid, List<MpTemplateParam> data) {
        this.touser = touser;
        this.template_id = template_id;
        this.url = url;
        this.appid = appid;
        this.data = data;
       /* JSONObject resData = new JSONObject();
        for (int i = 0; i < data.size(); i++) {
            if(i == 0){
                resData.put("first",JSON.toJSONString(data.get(i)));
            }else if(i == 1){
                resData.put("keyword1",JSON.toJSONString(data.get(i)));
            }else if(i == 2){
                resData.put("keyword2",JSON.toJSONString(data.get(i)));
            }else if(i == 3){
                resData.put("keyword3",JSON.toJSONString(data.get(i)));
            }else if(i == 4){
                resData.put("keyword4",JSON.toJSONString(data.get(i)));
            }else if(i == 5){
                resData.put("remark",JSON.toJSONString((data.get(i))));
            }
        }
        this.data = resData;*/
    }


    public String toJSON() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("{");
        buffer.append(String.format("\"touser\":\"%s\"", this.touser)).append(",");
        buffer.append(String.format("\"template_id\":\"%s\"", this.template_id)).append(",");
        if(StrUtil.isNotBlank(this.url)){
            buffer.append(String.format("\"url\":\"%s\"", this.url)).append(",");
        }
        if(this.miniprogram != null){
            buffer.append("\"miniprogram\":{");
            buffer.append(String.format("\"appid\":\"%s\"",this.miniprogram.getAppid())).append(",");
            buffer.append(String.format("\"pagepath\":\"%s\"",this.miniprogram.getPagepath()));
            buffer.append("}").append(",");
        }
        buffer.append("\"data\":{");
        MpTemplateParam param = null;
        String indexNum = null;
        for (int i = 0; i < this.data.size(); i++) {
            if(i == 0){
                indexNum = "first";
            }else if(i == 1){
                indexNum = "keyword1";
            }else if(i == 2){
                indexNum = "keyword2";
            }else if(i == 3){
                indexNum = "keyword3";
            }else if(i == 4){
                indexNum = "keyword4";
            }else if(i == 5){
                indexNum = "remark";
            }
            param = this.data.get(i);
            buffer.append(String.format("\"%s\":{",indexNum));
            buffer.append(String.format("\"value\":\"%s\"",param.getValue())).append(",");
            buffer.append(String.format("\"color\":\"%s\"",param.getColor()));
            buffer.append("}").append(",");
        }
        buffer.deleteCharAt(buffer.length()-1);     //去掉，
        buffer.append("}");
        buffer.append("}");
        return buffer.toString();
    }
}

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
class Miniprogram{
    private String appid;
    private String pagepath;
}